﻿using Furion.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Newtonsoft.Json;
using System;
using System.Runtime.Serialization;
using YFurion.Entities;

namespace UpperComputer.Core.Entities
{
    public class BaseRoleUser : CEntity, IEntityTypeBuilder<BaseRoleUser>
    {
        public string Account { get; set; }

        public string RoleCode { get; set; }

        [JsonIgnore]
        [IgnoreDataMember]
        public virtual BaseUser AccountNavigation { get; set; }

        public virtual BaseRole RoleCodeNavigation { get; set; }

        public void Configure(EntityTypeBuilder<BaseRoleUser> entity, DbContext dbContext, Type dbContextLocator)
        {
            entity.Property(e => e.Id)
                .ValueGeneratedNever()
                .HasComment("编号");

            entity.Property(e => e.Account)
                .IsRequired()
                .HasMaxLength(50)
                .IsUnicode(false)
                .HasComment("账号");

            entity.Property(e => e.CreationTime).HasComment("创建时间");

            entity.Property(e => e.CreatorUserId)
                .HasComment("创建者编号");

            entity.Property(e => e.RoleCode)
                .IsRequired()
                .HasMaxLength(50)
                .IsUnicode(false)
                .HasComment("角色编码");

            entity.HasOne(d => d.AccountNavigation)
                .WithMany(p => p.BaseRoleUser)
                .HasPrincipalKey(p => p.Account)
                .HasForeignKey(d => d.Account)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_BaseRoleUser_BaseUser");

            entity.HasOne(d => d.RoleCodeNavigation)
                .WithMany(p => p.BaseRoleUser)
                .HasPrincipalKey(p => p.Code)
                .HasForeignKey(d => d.RoleCode)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_BaseRoleUser_BaseRole");
        }
    }
}